﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComTools
{
    public partial class frmEncrypt : Form
    {
        public frmEncrypt()
        {
            InitializeComponent();
        }

        private void frmEncrypt_Load(object sender, EventArgs e)
        {
			
		}

        myCodeEncryption myEncrypt = new myCodeEncryption();
        private void button1_Click(object sender, EventArgs e)
        {
			try
			{
				textBox_GetCode.Text = GetRequestCode();
				if (this.textBox_GetCode.Text.Trim() != "")
				{
					string myInfo = this.myEncrypt.user_AES_Decrypt(this.textBox_GetCode.Text.Trim(), this.mykey);
					DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
					dtFormat.ShortDatePattern = "yyyy/MM/dd";
					string[] array = myInfo.Split(new char[]
					{
						'@'
					});
					string cpuID = array[0];
					string boardID = array[1];
					string hardID = array[2];
					DateTime regTime = Convert.ToDateTime(array[3], dtFormat);
					string regDay = array[4];
					int checkdate = int.Parse(DateDiff(regTime.AddDays(1.0), DateTime.Now));
					if (cpuID != this.myEncrypt.user_GetCPUID() || boardID != this.myEncrypt.user_GetMainBoardID()
						|| hardID != this.myEncrypt.user_GetHardID() || checkdate > 7)
					{
						MessageBox.Show("您所输入的授权码已失效！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					}
					else
					{
						myInfo = string.Concat(new string[]
						{
							regDay,
							"@",
							DateTime.Now.ToShortDateString(),
							"@",
							regTime.ToShortDateString(),
							"@",
							this.myEncrypt.user_GetCPUID(),
							"@",
							this.myEncrypt.user_GetHardID()
						});
						myInfo = this.myEncrypt.user_AES_Encrypt(myInfo, this.mykey);
						//MessageBox.Show("恭喜您已成功激活软件！使用期限至" + regTime.AddDays((double)int.Parse(regDay)).ToShortDateString(), "激活成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						SaveFileDialog dialog = new SaveFileDialog();
						dialog.Filter = "文本文件|*.txt|所有文件|*";
						dialog.AddExtension = true;
						dialog.DefaultExt = ".txt";
						dialog.FileName = "license.txt";
						dialog.SupportMultiDottedExtensions = false;
						if (dialog.ShowDialog() == DialogResult.OK)
						{
							var full = dialog.FileName;
							if (Path.GetFileName(full)!= "license.txt")
                            {
								full = Path.Combine(Path.GetDirectoryName(full), "license.txt");
                            }
							//File.WriteAllText(".\\license.txt", myInfo, Encoding.Default);
							File.WriteAllText(full, myInfo, Encoding.Default);
						}

					}
				}
			}
			catch(Exception exc)
			{
				MessageBox.Show(exc.Message, "错误信息");
			}
		}

        private string mykey = "#&RichardK911+-020IsG1e2n3i4u5s6";

		private string GetRequestCode()
		{
			string cpuID = this.myEncrypt.user_GetCPUID();
			string boardID = this.myEncrypt.user_GetMainBoardID();
			string hardID = this.myEncrypt.user_GetHardID();
			string miwen = string.Concat(new string[]
			{
				cpuID,
				"@",
				boardID,
				"@",
				hardID,
				"@",
				DateTime.Now.AddDays(7).ToShortDateString(),
				"@",
				"730"
			});
			var requestCode = this.myEncrypt.user_AES_Encrypt(miwen, this.mykey);
			return requestCode;
		}

		public string DateDiff(DateTime DateTime1, DateTime DateTime2)
		{
			int num = DateTime.Compare(DateTime1, DateTime2);
			TimeSpan ts = new TimeSpan(DateTime1.Ticks);
			TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
			TimeSpan ts3 = default(TimeSpan);
			if (num > 0)
			{
				ts3 = ts.Subtract(ts2).Duration();
			}
			else
			{
				ts3 = ts2.Subtract(ts).Duration();
			}
			return ts3.Days.ToString();
		}

		private void button_OK_Click(object sender, EventArgs e)
		{
			
		}
	}

}